package com.weixiaodong.ModelDesign.适应设计模式.Adapter模式.练习;


import com.weixiaodong.ModelDesign.适应设计模式.Adapter模式.适配器模式演示案例.使用委托的适配器.Print;

import java.io.*;
import java.util.HashMap;
import java.util.Properties;

@SuppressWarnings("all")
public class main {

    // 导出指定路径中的配置文件
    public static HashMap<String, String> loadProrities(String path) throws IOException {
        Properties properties = new Properties();

        Reader reader = null;

        HashMap<String, String> resMap = new HashMap<>();

        try {
            reader = new InputStreamReader(new FileInputStream(path));

            properties.load(reader); // 将文件中的属性读取到指定的reader流中，然后从流中将属性设置到properite对象中

            resMap.put("name" ,properties.getProperty("name"));
            resMap.put("email", properties.getProperty("email"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        return resMap;
    }



    public static void storeProrities(HashMap<String, String> prorityMap, String path, String comment) throws IOException {
        Properties properties = new Properties();
        for(String key : prorityMap.keySet()) {
            properties.setProperty(key, prorityMap.get(key));
        }

        Writer writer = null;

        try {
            writer = new OutputStreamWriter(new FileOutputStream(path));

            properties.store(writer, comment);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }


    public static void main(String[] args) throws IOException {
        String fileName = "/Users/weixiaodong/java_work/modeldesign/src/com/weixiaodong/tmp/file";


        // FileProperties就是对Properties通过接口FileIO的适配对象
        FileIO fileIO = new FileProperties(new Properties());

        fileIO.readFromFile(fileName);

        fileIO.setValue("month", "4");

        fileIO.setValue("day", "23");

        fileIO.writeToFile(fileName);
    }
}
